package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.BaseCategory2;
import com.atguigu.gmall.product.mapper.BaseCategory2Mapper;
import com.atguigu.gmall.product.service.BaseCategory2Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 搭忆长安
 * @version 1.0
 * @date 2021年06月25日 19:48
 * @ClassName:BaseCategory1ServiceImpl
 * @Description: TODO
 */
@Service
public class BaseCategory2ServiceImpl extends ServiceImpl<BaseCategory2Mapper, BaseCategory2>implements BaseCategory2Service {

    @Autowired
    private BaseCategory2Mapper baseCategory2Mapper;

    /**
     * 条件查询
     * @param baseCategory2
     * @return
     */
    @Override
    public List<BaseCategory2> search(BaseCategory2 baseCategory2) {
        //判断查询参数是否为空
        if(baseCategory2==null){
            //若没有条件查询全部
            return baseCategory2Mapper.selectList(null);
        }
        LambdaQueryWrapper wrapper=getCondition(baseCategory2);
        //执行查询
        List<BaseCategory2> list = baseCategory2Mapper.selectList(wrapper);
        //返回结果
        return list;
    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public List<BaseCategory2> pageSearch(Integer page, Integer size) {
        //执行查询
        IPage<BaseCategory2> result = baseCategory2Mapper.selectPage(new Page<>(page, size), null);
        //返回结果
        return result.getRecords();
    }

    /**
     * 条件分页查询
     * @param baseCategory2
     * @param page
     * @param size
     * @return
     */
    @Override
    public List<BaseCategory2> search(BaseCategory2 baseCategory2, Integer page, Integer size) {
        //构建查询条件
        LambdaQueryWrapper wrapper=getCondition(baseCategory2);
        //分页查询
        IPage<BaseCategory2> iPage = baseCategory2Mapper.selectPage(new Page<>(page, size), wrapper);
        //返回结果
        return iPage.getRecords();
    }

    private LambdaQueryWrapper getCondition(BaseCategory2 baseCategory2){
        //定义条件构造器
        LambdaQueryWrapper<BaseCategory2> wrapper = new LambdaQueryWrapper<>();
        //若id不为空
        if(baseCategory2.getId()!=null){
            wrapper.eq(BaseCategory2::getId,baseCategory2.getId());
        }
        //若name不为空
        if(StringUtils.isNotEmpty(baseCategory2.getName())){
            wrapper.like(BaseCategory2::getName,baseCategory2.getName());

        }
        return wrapper;
    }
}
